package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.TaskOrderBoxAdapter;
import com.mflex.WHSmartWhouseAd.adapter.TaskQueryBoxAdapter;
import com.mflex.WHSmartWhouseAd.model.CommitBOXLinkPlateModel;
import com.mflex.WHSmartWhouseAd.model.SpotCheckPdaInfor;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.util.ArrayList;
import java.util.List;

//import butterknife.BindView;
//import butterknife.ButterKnife;


public class QMSTaskQueryActivity extends BaseActivity implements AdapterView.OnItemClickListener  {
  // 操作人员

    EditText stvOperator;
    // 载具码

    EditText stvVehicleCode;
    // 列表

    ListView lvList;

    // loading
    MiniLoadingDialog miniLoadingDialog;
    List<SpotCheckPdaInfor>TaskQueryModelList=new ArrayList<>();
    CommitBOXLinkPlateModel model = new CommitBOXLinkPlateModel();
    private String userName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_qms_task_query);
//        ButterKnife.bind(this);
        stvOperator=findViewById(R.id. stv_Operator);
        stvVehicleCode=findViewById(R.id.stv_VehicleCode);
        lvList=findViewById(R.id.lvList);
        // 弹窗提示
        miniLoadingDialog =WidgetUtils.getMiniLoadingDialog(this,"加载...");
        // 传递数据
        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        // 视图按键监听器
        stvVehicleCode.setOnKeyListener(this);
        lvList.setOnItemClickListener(this);
        // 界面跳转
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(QMSTaskQueryActivity.this,QMSSamplingInspectionActivity.class);
                intent.putExtra("TaskNo", TaskQueryModelList.get(position).getTaskNo());
                intent.putExtra("MPart",TaskQueryModelList.get(position).getMPart());
                intent.putExtra("Qty",String.valueOf(TaskQueryModelList.get(position).getQty()));
                intent.putExtra("User",stvOperator.getText().toString());
                intent.putExtra("VehicleNumber",stvVehicleCode.getText().toString());
                intent.putExtra("MaterialType",TaskQueryModelList.get(position).getMaterialType());
//                startActivity(intent);
                startActivityForResult(intent,0);

            }
        });
    }
    @Override
    public void onClick(View view) {

    }
    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN && (i == 66 || i == 61)) {
            switch (view.getId()) {
                case R.id.stv_VehicleCode:// 载具码
                    chekVehicleCode();
                    break;
                default:
                    break;
            }
        }
        return false;
    }
// 载具码回车事件触发接口
    private void chekVehicleCode() {
        if(StringUtils.isEmpty(stvOperator.getText().toString())) {
            MediaPlayer mp = MediaPlayer.create(QMSTaskQueryActivity.this,R.raw.error_voice);mp.start();
            new MaterialDialog.Builder(QMSTaskQueryActivity.this)
             .iconRes(R.drawable.icon_warning)
             .title(R.string.tip_warning)
             .content("操作人未输入")
             .positiveText(R.string.lab_submit)
             .show();
            return;
        }
        if(StringUtils.isEmpty(stvVehicleCode.getText().toString())) {
            return;
        }
//       测试
//        SpotCheckPdaInfor people = new SpotCheckPdaInfor();
//        double adm =   Math.random();
//        people.setTaskNo("123"+adm);
//        people.setMaterialType("55555555");
//        people.setStatus("ok");
//        List dataList = new ArrayList<>();
//        dataList.add(people);
//        TaskQueryModelList = dataList;
//        TaskQueryBoxAdapter TaskQueryBoxAdapter = new TaskQueryBoxAdapter(QMSTaskQueryActivity.this,TaskQueryModelList);
//        lvList.setAdapter(TaskQueryBoxAdapter);
//        结束
     miniLoadingDialog.show();
     XHttp.get("api/WHSmartWhousePDA/GetQMSTaskBind")
             .params("vehiclenumber",stvVehicleCode.getText().toString())
             .syncRequest(false)
             .onMainThread(true)
             .execute(new SimpleCallBack<List<SpotCheckPdaInfor>>() {
                 @Override
                 public  void  onSuccess(List<SpotCheckPdaInfor> response) {
                     miniLoadingDialog.dismiss();
                     TaskQueryModelList = response;
                     Log.d("response",response.toString());
                     TaskQueryBoxAdapter TaskQueryBoxAdapter = new TaskQueryBoxAdapter(QMSTaskQueryActivity.this,TaskQueryModelList);

                     lvList.setAdapter(TaskQueryBoxAdapter);

                 }
                 @Override
                 public void onError(ApiException e) {
                     miniLoadingDialog.dismiss();
                     MediaPlayer mp = MediaPlayer.create(QMSTaskQueryActivity.this,R.raw.error_voice);mp.start();
                     new MaterialDialog.Builder(QMSTaskQueryActivity.this)
                             .iconRes(R.drawable.icon_warning)
                             .title(R.string.tip_warning)
                             .content(e.getMessage())
                             .positiveText(R.string.lab_submit)
                             .show();
                 }
             });
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}